package com.zpl.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zpl.order.entity.Cart;
import com.zpl.order.entity.dto.CartKeyWordDTO;
import com.zpl.order.mapper.CartMapper;
import com.zpl.order.service.CartService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CartServiceImpl implements CartService {

    @Resource
    private CartMapper cartMapper;


    @Override
    public boolean insert(Cart cart) {
        if(cartMapper.insert(cart)>0){
            return true;
        }
        return false;
    }

    @Override
    public boolean delete(Long id) {
        if(cartMapper.deleteById(id)>0){
            return true;
        }
        return false;
    }

    @Override
    public List<Cart> selectByChecked(Long memberId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("member_id", memberId);
        queryWrapper.eq("checked", "1");
        List<Cart> list = cartMapper.selectList(queryWrapper);
        return list;
    }

    @Override
    public boolean deleteByChecked(Long memberId) {

        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("member_id", memberId);
        queryWrapper.eq("checked", "1");

        if(cartMapper.delete(queryWrapper)>0){
            return true;
        }
        return false;
    }

    @Override
    public boolean update(Cart cart) {
        if(cartMapper.updateById(cart)>0){
            return true;
        }
        return false;
    }

    @Override
    public Cart selectById(Long id) {
        return cartMapper.selectById(id);
    }

    @Override
    public IPage<Cart> getCartByMemberId(CartKeyWordDTO cartKeyWordDTO) {
        Page page = new Page<>(cartKeyWordDTO.getCurrentPage(), cartKeyWordDTO.getPageSize());
        QueryWrapper queryWrapper = new QueryWrapper();
        // 条件
        if (!StringUtils.isEmpty(cartKeyWordDTO.getMemberId())) {
            queryWrapper.eq("member_id", cartKeyWordDTO.getMemberId());
            IPage<Cart> cartIPage = cartMapper.selectPage(page, queryWrapper);
            return cartIPage;
        }else {
            return null;
        }

    }
}
